package com.loveprogrammer.eternity;

import com.loveprogrammer.eternity.action.BaseAction;
import com.loveprogrammer.eternity.action.SimpleStart;
import com.loveprogrammer.eternity.factory.HeroFactory;
import com.loveprogrammer.eternity.factory.template.HeroTemplate;
import com.loveprogrammer.eternity.model.game.GameWrapper;
import com.loveprogrammer.eternity.model.game.HeroWrapper;
import com.loveprogrammer.eternity.utils.LoadGame;
import com.loveprogrammer.eternity.utils.ScannerInput;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @version 1.0.0
 * @description: 主入口
 * @author: eric
 * @date: 2022-08-18 18:08
 **/
public class Main extends BaseAction {

    private static GameWrapper gameWrapper;

    public static void main(String[] args) {
        try {
            // 读取存档 只有一个档 多个人物
            // String buffer = Util.readFileContentAsBuffer("save/save.json");
            // gameWrapper = JSON.parseObject(buffer,GameWrapper.class);
            gameWrapper = LoadGame.load();
            // 开始界面
            eventManager.notifyMessage("欢迎回来，请选择您要进行的操作", 1);
            eventManager.notifyMessage("[1]-创建新人物   [2]-选择已有人物  [3]-退出", 1);
            System.out.print("你的选择是：");
            // 自动存档
//            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
//                eventManager.notifyMessage("Shutdown hook called auto save game");
//                if(gameWrapper != null){
//                    LoadGame.save(gameWrapper);
//                }
//            }));

            int choose = ScannerInput.inputInt(1,3,3);
            switch (choose){
                case 1:
                    // 请选择英雄
                    HeroTemplate heroTemplate = HeroFactory.selectHero();

                    eventManager.notifyMessage("请输入英雄的大名", 1);

                    Scanner nameScanner = new Scanner(System.in);
                    String name = nameScanner.nextLine();

                    HeroWrapper heroWrapper = HeroFactory.createHero(heroTemplate,name);
                    if(gameWrapper == null){
                        gameWrapper = new GameWrapper();
                        gameWrapper.setHeroWrapperMap(new HashMap<>());
                    }
                    gameWrapper.getHeroWrapperMap().put(name,heroWrapper);
                    SimpleStart simpleStart = new SimpleStart();
                    simpleStart.start(heroWrapper,gameWrapper);
                    break;
                case 2:
                    if(gameWrapper.getHeroWrapperMap() != null){
                        eventManager.notifyMessage("请选择已有的英雄", 1);
                        Map<Integer,HeroWrapper> map = new HashMap<>();
                        int i = 1;
                        for (Map.Entry<String, HeroWrapper> entry : gameWrapper.getHeroWrapperMap().entrySet()) {
                            eventManager.notifyMessage(i + " 姓名" + entry.getKey() + ",英雄：" + entry.getValue().getAliasName() + " 等级：" + entry.getValue().getHero().getLevel(), 1);
                            eventManager.notifyMessage(" 金钱: " + entry.getValue().getWarehouse().getGoldCoin(), 1);

                            map.put(i,entry.getValue());
                            i++;
                        }
                        eventManager.notifyMessage("输入0 返回", 1);

                        int hero = ScannerInput.inputInt(1,map.size(),0);
                        if(hero == 0){
                            break;
                        }
                        HeroWrapper wrapper = map.get(hero);
                        simpleStart = new SimpleStart();
                        // 更新任务进度
                        eventManager.notifyMessage("更新任务进度", 1);
                        mainTaskService.updateMissionStatus(wrapper.getTaskManager());
                        simpleStart.start(wrapper, gameWrapper);
                    }
                    break;
                case 3:
                    eventManager.notifyMessage("再见", 1);
                    break;
                default:
                    break;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

}
